package servlets;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import actionForm.GetContactByIdValidationForm;

import domain.IDAOContactGroup;

public class DeleteContactGroupAction extends Action {
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse response) {
		if (request.getMethod().equals("POST")) {
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServlet().getServletContext());
			IDAOContactGroup dao = (IDAOContactGroup) context.getBean("daocontactgroup");
			GetContactByIdValidationForm form = (GetContactByIdValidationForm) pForm;
			String id =form.getId();
			boolean success = dao.deleteContactGroup(Long.valueOf(id));
			if(success){
				request.setAttribute("successMessage", "The contact group has been deleted.");
				return pMapping.findForward("success");
			} else {
				request.setAttribute("errorMessage", "The contact group could not be deleted.");
				return pMapping.findForward("error");
			}
			
		} else if (request.getMethod().equals("GET")) {
			request.setAttribute("groupId", request.getParameter("id"));
			return pMapping.findForward("confirmation");
		}
		return null;
	}
}
